//
//  GAKKIBasePageVesselViewController.m
//  GANGAIKit
//
//  Created by fh on 2019/11/15.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBasePageVesselViewController.h"

@interface GAKKIBasePageVesselViewController () <UIScrollViewDelegate,SGPageTitleViewDelegate> {
    NSInteger _lastSelectedIndex;
}
@property (nonatomic, readwrite, strong) UIScrollView *pageContentScrollView;
@property (nonatomic, assign) GAKKIBasePageVesselViewStyle style;
@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@end

@implementation GAKKIBasePageVesselViewController

#pragma mark - OverWrite
+ (instancetype)new {
    return [super new];
}
- (instancetype)init {
    if (self = [super init]) {
        [self baseBasePageVesselViewControllerDefaultInitSetup];
        self.style = GAKKIBasePageVesselViewStyleTopScrollBar;
    }
    return self;
}
- (instancetype)initWithParams:(NSDictionary *)params {
    return [self initWithStyle:GAKKIBasePageVesselViewStyleTopScrollBar params:params];
}
- (void)setupView {
    CGFloat tabH = self.tabBarController ? kApplicationTabBarHeight : kApplicationBottomSpace;
    tabH = (self.tabBarController.tabBar.isHidden | self.hidesBottomBarWhenPushed) ? kApplicationBottomSpace : tabH;
    switch (self.style) {
        case GAKKIBasePageVesselViewStyleTopScrollBar: {
            CGFloat y = [self innerLayoutSGPagingView]; // y是菜单栏高度
            if (y == -1) {
                return;
            }
            [self.view addSubview:self.pageContentScrollView];
            self.pageContentScrollView.frame = CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height-y-tabH);
            [self innerLayoutSubViewControllers];
            break;
        }
        case GAKKIBasePageVesselViewStyleCustom: {
            [self customSetupView];
            break;
        }
    }
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    _backgroundColor = backgroundColor;
    self.pageContentScrollView.backgroundColor = backgroundColor;
    self.view.backgroundColor = backgroundColor;
}

#pragma mark - Public
- (instancetype)initWithStyle:(GAKKIBasePageVesselViewStyle)style {
    return [self initWithStyle:style params:nil];
}
- (instancetype)initWithStyle:(GAKKIBasePageVesselViewStyle)style params:(NSDictionary * _Nullable)params {
    return [self initWithStyle:style params:params dataSource:nil];
}
- (instancetype)initWithStyle:(GAKKIBasePageVesselViewStyle)style params:(NSDictionary * _Nullable)params dataSource:(NSMutableArray<GAKKIBaseViewController *> * _Nullable)dataSource {
    if (self = [super initWithParams:params]) {
        [self baseBasePageVesselViewControllerDefaultInitSetup];
        self.style = style;
        if (dataSource && dataSource.count > 0) {
            self.childPageControllers = dataSource;
        }
    }
    return self;
}
- (void)baseBasePageVesselViewControllerDefaultInitSetup {
    self.shouldAutoConfigureViewOnViewDidLoad = NO;
    self.shouldRequestRemoteDataOnViewDidLoad = NO;
    self.prefersNavigationBarHidden = NO;
}
- (void)customSetupView {
    // 提供给子类重写的
}
- (void)configDataSource {
    // 提供给子类重写的
}

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configDataSource];
    [self setupView];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self childControllerDidAppearWithChildControllerIndex:0];
}

#pragma mark - BasePageVesselViewControllerDataSource
- (GAKKIBaseViewController *)basePageVesselViewController:(GAKKIBasePageVesselViewController *)basePageVesselViewController subViewControllerForIndex:(NSInteger)index {
    return self.childPageControllers[index];
}
- (NSInteger)numberOfSubViewControllerForBasePageVesselViewController:(GAKKIBasePageVesselViewController *)basePageVesselViewController {
    return self.childPageControllers.count;
}

#pragma mark - Event

#pragma mark - SGPageTitleViewDelegate
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    [self innerRefreshPageContentScrollViewOffsetWithCurrentSelectedIndex:selectedIndex];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.pageContentScrollView]) {
        NSInteger selectedIndex = scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame);
        self.pageTitleView.resetSelectedIndex = selectedIndex;
    }
}

#pragma mark - Private
- (void)innerLayoutSubViewControllers {
    for (NSInteger i = 0; i < self.childPageControllers.count; i++) {
        UIViewController *childVC = self.childPageControllers[i];
        [self addChildViewController:childVC];
        childVC.view.frame = CGRectMake(CGRectGetWidth(self.pageContentScrollView.frame) * i, 0, CGRectGetWidth(self.pageContentScrollView.frame), CGRectGetHeight(self.pageContentScrollView.frame));
        [self.pageContentScrollView addSubview:childVC.view];
        [childVC didMoveToParentViewController:self];
    }
    self.pageContentScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.pageContentScrollView.frame) * self.childPageControllers.count, self.pageContentScrollView.frame.size.height);
    [self.pageContentScrollView scrollRectToVisible:CGRectMake(0, 0, self.pageContentScrollView.frame.size.width, self.pageContentScrollView.frame.size.height) animated:YES];
}

- (CGFloat)innerLayoutSGPagingView {
    if (self.childPageControllers.count < 1 || !self.childPageControllers) {
        return -1;
    }
    
    NSMutableArray<NSString *> *titleArr = [NSMutableArray array];
    for (GAKKIBaseViewController *subVC in self.childPageControllers) {
        if (kStringIsEmpty(subVC.pageTitle)) {
            [titleArr addObject:@"null"];
        }else {
            [titleArr addObject:subVC.pageTitle];
        }
    }
    // 数据不对都不初始化视图
    if (titleArr.count < 1 || titleArr.count != self.childPageControllers.count) {
        return -1;
    }

    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.titleSelectedColor = [UIColor redColor]; // um_subscribe_detailTimeColor
    configure.titleColor = [UIColor blueColor];
    configure.titleFont = [UIFont fontWithName:@"Helvetica" size:16.f];
    configure.bottomSeparatorColor = [UIColor cyanColor];
    configure.indicatorColor = [UIColor purpleColor]; // um_subscribe_detailTimeColor
    configure.titleMinWidth = 84;
    
    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44) delegate:self titleNames:titleArr configure:configure];
    _pageTitleView.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:self.pageTitleView];
    
    return CGRectGetMaxY(self.pageTitleView.frame);
}

- (void)innerRefreshPageContentScrollViewOffsetWithCurrentSelectedIndex:(NSInteger)selectedIndex {
    CGFloat offsetX = CGRectGetWidth(self.pageContentScrollView.frame) * selectedIndex;
    [self.pageContentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    if (_lastSelectedIndex != selectedIndex) {
        [self childControllerDidAppearWithChildControllerIndex:selectedIndex];
        _lastSelectedIndex = selectedIndex;
    }
}

- (void)childControllerDidAppearWithChildControllerIndex:(NSInteger)childControllerIndex {
    // 子类复写获取生命周期
}

#pragma mark - Setter and getter
- (UIScrollView *)pageContentScrollView {
    if (!_pageContentScrollView) {
        _pageContentScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _pageContentScrollView.delegate = self;
        _pageContentScrollView.pagingEnabled = YES;
        _pageContentScrollView.bounces = NO;
        _pageContentScrollView.showsHorizontalScrollIndicator = NO;
        _pageContentScrollView.showsVerticalScrollIndicator = NO;
//        _pageContentScrollView.scrollEnabled = YES; // from产品: 用户体验不给滚动
    }
    return _pageContentScrollView;
}
- (NSMutableArray<GAKKIBaseViewController *> *)childPageControllers {
    if (!_childPageControllers) {
        _childPageControllers = [NSMutableArray array];
    }
    return _childPageControllers;
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    if (currentIndex > self.childPageControllers.count - 1) {
        currentIndex = self.childViewControllers.count - 1;
    }else if (currentIndex < 0) {
        currentIndex = 0;
    }
    self.pageTitleView.resetSelectedIndex = currentIndex;
}

@end
